package _2TCP;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;

public class TCPClient {

	public static void main(String[] args) throws IOException {
		
	
		
		// 1. 新建Socket
		String host = "127.0.0.1";
		int port = 7777;
		System.out.println("客户端:连接服务器"+host+":"+port);
		Socket s = new Socket(host,port);
		
		// 2.
		System.out.println("已连上服务器");
		InputStream in = s.getInputStream();
		OutputStream out = s.getOutputStream();
		
		// 3. 读取信息
		// 接收服务器发来的信息
		byte[] buf = new byte[1024];
		int len = in.read(buf);
		String msg = new String(buf,0,len);
		System.out.println("服务器发来:"+msg);
		
		// 给服务器发个信息
		out.write("hello!".getBytes());
		
		// 4. 关闭连接
		s.close();
	}
}
